package org.sixtynine.stock.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.Module;
import org.sixtynine.stock.entity.User;
import org.sixtynine.stock.model.LoadingSearchCriteria;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.ModuleService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 *         this class use to add , edit and delete module data
 * 
 */

@Controller
@SessionAttributes
public class ModuleController extends BackEndAuthController {

	private int RECORDS_PER_PAGE = 10;

	@Autowired
	private GenericService genericService;

	@Autowired
	DataManagerService dataManagerService;

	@Autowired
	ModuleService moduleService;

	@RequestMapping(value = "/admin/module_data/search")
	public ModelAndView search(@ModelAttribute LoadingSearchCriteria criteria) {
		ModelAndView modelAndView = new ModelAndView("/admin/module_data/list");
		loadData(1, -1, criteria.getName(), modelAndView, criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/module_data/add")
	public ModelAndView addModule(HttpServletRequest request) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/module_data/add");
		modelAndView.addObject("module_data", new Module());
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/module_data/add/process")
	public ModelAndView addingModule(@ModelAttribute @Valid Module module_data,
			BindingResult result) {
		ModelAndView modelAndView = null;
		if (!result.hasErrors()) {
			genericService.saveOrUpdate(module_data);
			modelAndView = new ModelAndView("/admin/module_data/list");

			loadData(1, -1, null, modelAndView, null);

			String message = "Module was successfully added.";
			modelAndView.addObject("message", message);

		} else {
			modelAndView = new ModelAndView("/admin/module_data/add");
			return modelAndView;
		}

		return modelAndView;
	}

	@RequestMapping(value = "/admin/module_data/list")
	public ModelAndView listOfModule(
			HttpServletRequest request,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "totalRecords", required = false) Integer totalRecords) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/module_data/list");
		loadData(page, totalRecords, null, modelAndView, null);
		modelAndView.addObject("userLogged", true);
		return modelAndView;
	}

	/**
	 * load data
	 * 
	 * @param page
	 * @param totalRecords
	 * @param companyId
	 * @param fromDate
	 * @param toDate
	 * @param modelAndView
	 */
	private void loadData(Integer page, Integer totalRecords, String name,
			ModelAndView modelAndView, LoadingSearchCriteria criteria) {
		GridPramDTO gridPramDTO = new GridPramDTO();
		gridPramDTO.setRows(RECORDS_PER_PAGE);
		if (page != null) {
			gridPramDTO.setPage(page);
		} else {
			gridPramDTO.setPage(1);
		}
		if (totalRecords != null) {
			gridPramDTO._setTotalNumberOfRecords(totalRecords);
		}

		Page<Module> modulesPage = moduleService
				.searchModule(name, gridPramDTO);
		modelAndView.addObject("modulesPage", modulesPage);

		if (criteria == null) {
			criteria = new LoadingSearchCriteria();
			criteria.setName(name);
		}
		modelAndView.addObject("criteria", criteria);

	}

	@RequestMapping(value = "/admin/module_data/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editModule(HttpServletRequest request,
			@PathVariable Integer id) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/module_data/edit");
		BaseEntity module = genericService.findById(id, Module.class);
		modelAndView.addObject("module_data", module);
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/module_data/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingModule(
			@ModelAttribute @Valid Module module_data, BindingResult result,
			@PathVariable Integer id) {
		ModelAndView modelAndView = null;
		if (!result.hasErrors()) {
			genericService.saveOrUpdate(module_data);
			modelAndView = new ModelAndView("/admin/module_data/list");

			loadData(1, -1, module_data.getName(), modelAndView, null);

			String message = "Module was successfully edited.";
			modelAndView.addObject("message", message);

		} else {
			modelAndView = new ModelAndView("/admin/module/edit");

		}

		return modelAndView;
	}

	@RequestMapping(value = "/admin/module/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteModule(@PathVariable Integer id) {
		genericService.delete(id, Module.class);
		ModelAndView modelAndView = new ModelAndView("/admin/module/list");

		loadData(1, -1, null, modelAndView, null);

		List<BaseEntity> modules = genericService.findAll(Module.class);
		modelAndView.addObject("modules", modules);
		String message = "Module was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}
}
